return PEAR::raiseError(null, QUICKFORM_INVALID_DATASOURCE, null, E_USER_WARNING, "Datasource is not an object in QuickForm::setDatasource()", 'HTML_QuickForm_Error', true);
}
} // end func setDatasource
// }}}
// {{{ setDefaults()
/**
* Initializes default form values
*
* @param array $defaultValues values used to fill the form
* @param mixed $filter (optional) filter(s) to apply to all default values
* @since 1.0
* @access public
* @return void
*/
function setDefaults($defaultValues = null, $filter = null)
{
if (is_array($defaultValues)) {
if (isset($filter)) {
if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
foreach ($filter as $val) {
if (!is_callable($val)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setDefaults()", 'HTML_QuickForm_Error', true);
return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setDefaults()", 'HTML_QuickForm_Error', true);
* These values won't get overridden by POST or GET vars
*
* @param array $constantValues values used to fill the form
* @param mixed $filter (optional) filter(s) to apply to all default values
*
* @since 2.0
* @access public
* @return void
*/
function setConstants($constantValues = null, $filter = null)
{
if (is_array($constantValues)) {
if (isset($filter)) {
if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
foreach ($filter as $val) {
if (!is_callable($val)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setConstants()", 'HTML_QuickForm_Error', true);
return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setConstants()", 'HTML_QuickForm_Error', true);
* @param string $event event to send to newly created element ('createElement' or 'addElement')
* @param string $type element type
* @param array $args arguments for event
* @since 2.0
* @access private
* @return object a new element
* @throws HTML_QuickForm_Error
*/
function &_loadElement($event, $type, $args)
{
$type = strtolower($type);
if (!HTML_QuickForm::isTypeRegistered($type)) {
return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, "Element '$type' does not exist in HTML_QuickForm::_loadElement()", 'HTML_QuickForm_Error', true);
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElement()", 'HTML_QuickForm_Error', true);
}
} // end func getElement
// }}}
// {{{ &getElementValue()
/**
* Returns the element's raw value
*
* This returns the value as submitted by the form (not filtered)
* or set via setDefaults() or setConstants()
*
* @param string $element Element name
* @since 2.0
* @access public
* @return mixed element value
* @throws HTML_QuickForm_Error
*/
function &getElementValue($element)
{
if (!isset($this->_elementIndex[$element])) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElementValue()", 'HTML_QuickForm_Error', true);
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$elementName' does not exist in HTML_QuickForm::removeElement()", 'HTML_QuickForm_Error', true);
}
} // end func removeElement
// }}}
// {{{ addRule()
/**
* Adds a validation rule for the given field
*
* If the element is in fact a group, it will be considered as a whole.
* To validate grouped elements as separated entities,
* use addGroupRule instead of addRule.
*
* @param string $element Form element name
* @param string $message Message to display for invalid data
* @param string $type Rule type, use getRegisteredRules() to get types
* @param string $format (optional)Required for extra rule data
* @param string $validation (optional)Where to perform validation: "server", "client"
* @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
* @param boolean $force Force the rule to be applied, even if the target form element does not exist
if (!is_array($element) && !$this->elementExists($element)) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true);
} elseif (is_array($element)) {
foreach ($element as $el) {
if (!$this->elementExists($el)) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$el' does not exist in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true);
}
}
}
}
if (false === ($newName = $this->isRuleRegistered($type, true))) {
return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true);
} elseif (is_string($newName)) {
$type = $newName;
}
if (is_array($element)) {
$dependent = $element;
$element = array_shift($dependent);
} else {
$dependent = null;
}
if ($type == 'required' || $type == 'uploadedfile') {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Group '$group' does not exist in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
if (false === ($newName = $this->isRuleRegistered($type, true))) {
return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
} elseif (is_string($newName)) {
$type = $newName;
}
$this->_rules[$elementName][] = array(
'type' => $type,
'format' => $format,
'message' => $rule[0],
'validation' => $validation,
'reset' => $reset,
'group' => $group);
if ('required' == $type || 'uploadedfile' == $type) {
if ($required > 0 && count($groupObj->getElements()) == $required) {
$this->_required[] = $group;
}
} elseif (is_string($arg1)) {
if (false === ($newName = $this->isRuleRegistered($type, true))) {
return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
} elseif (is_string($newName)) {
$type = $newName;
}
// Radios need to be handled differently when required
if ($type == 'required' && $groupObj->getGroupType() == 'radio') {
* This should be used when for a rule involving several fields or if
* you want to use some completely custom validation for your form.
* The rule function/method should return true in case of successful
* validation and array('element name' => 'error') when there were errors.
*
* @access public
* @param mixed Callback, either function name or array(&$object, 'method')
* @throws HTML_QuickForm_Error
*/
function addFormRule($rule)
{
if (!is_callable($rule)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, 'Callback function does not exist in HTML_QuickForm::addFormRule()', 'HTML_QuickForm_Error', true);
}
$this->_formRules[] = $rule;
}
// }}}
// {{{ applyFilter()
/**
* Applies a data filter for the given field(s)
*
* @param mixed $element Form element name or array of such names
* @param mixed $filter Callback, either function name or array(&$object, 'method')
* @since 2.0
* @access public
*/
function applyFilter($element, $filter)
{
if (!is_callable($filter)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::applyFilter()", 'HTML_QuickForm_Error', true);
if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) {
if (is_array($res)) {
$this->_errors += $res;
} else {
return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, 'Form rule callback returned invalid value in HTML_QuickForm::validate()', 'HTML_QuickForm_Error', true);
}
}
}
return (0 == count($this->_errors));
} // end func validate
// }}}
// {{{ freeze()
/**
* Displays elements without HTML input tags
*
* @param mixed $elementList array or string of element(s) to be frozen
* @since 1.0
* @access public
* @throws HTML_QuickForm_Error
*/
function freeze($elementList=null)
{
$elementFlag = false;
if (isset($elementList) && !is_array($elementList)) {
$elementList = split('[ ]*,[ ]*', $elementList);
} elseif (!isset($elementList)) {
$this->_freezeAll = true;
}
foreach ($this->_elements as $key => $val) {
// need to get the element by reference
$element = &$this->_elements[$key];
if (is_object($element)) {
$name = $element->getName();
if ($this->_freezeAll || in_array($name, $elementList)) {
$elementFlag = true;
$element->freeze();
}
}
}
if (!$elementFlag) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::freeze()", 'HTML_QuickForm_Error', true);
}
return true;
} // end func freeze
// }}}
// {{{ isFrozen()
/**
* Returns whether or not the whole form is frozen
*
* @since 3.0
* @access public
* @return boolean
*/
function isFrozen()
{
return $this->_freezeAll;
} // end func isFrozen
// }}}
// {{{ process()
/**
* Performs the form data processing
*
* @param mixed $callback Callback, either function name or array(&$object, 'method')
* @param bool $mergeFiles Whether uploaded files should be processed too
* @since 1.0
* @access public
* @throws HTML_QuickForm_Error
*/
function process($callback, $mergeFiles = true)
{
if (!is_callable($callback)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_PROCESS, null, E_USER_WARNING, "Callback function does not exist in QuickForm::process()", 'HTML_QuickForm_Error', true);
* The description of the array structure is in HTML_QuickForm_Renderer_Array docs
*
* @since 2.0
* @access public
* @param bool Whether to collect hidden elements (passed to the Renderer's constructor)
* @return array of form contents
*/
function toArray($collectHidden = false)
{
include_once 'HTML/QuickForm/Renderer/Array.php';
$renderer =& new HTML_QuickForm_Renderer_Array($collectHidden);
$this->accept($renderer);
return $renderer->toArray();
} // end func toArray
// }}}
// {{{ exportValue()
/**
* Returns a 'safe' element's value
*
* This method first tries to find a cleaned-up submitted value,
* it will return a value set by setValue()/setDefaults()/setConstants()
* if submitted value does not exist for the given element.
*
* @param string Name of an element
* @access public
* @return mixed
*/
function exportValue($element)
{
if (!isset($this->_elementIndex[$element])) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElementValue()", 'HTML_QuickForm_Error', true);